package com.cj.bus.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class DateUtil {

   private static DateTimeFormatter  df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
   private static DateTimeFormatter  dfYMD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
   private static DateTimeFormatter  dfYM = DateTimeFormatter.ofPattern("yyyyMM");

    private static DateTimeFormatter[] dfMinList = new DateTimeFormatter[]{
            DateTimeFormatter.ofPattern("H:m"),
            DateTimeFormatter.ofPattern("H:mm"),
            DateTimeFormatter.ofPattern("HH:m"),
            DateTimeFormatter.ofPattern("HH:mm")
    };

    private static DateTimeFormatter[] dfList = new DateTimeFormatter[]{
           DateTimeFormatter.ofPattern("yyyy年MM月dd日 H:m"),
           DateTimeFormatter.ofPattern("yyyy年MM月dd日 H:mm"),
           DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:m"),
           DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm"),

           DateTimeFormatter.ofPattern("yyyy年M月dd日 H:m"),
           DateTimeFormatter.ofPattern("yyyy年M月dd日 H:mm"),
           DateTimeFormatter.ofPattern("yyyy年M月dd日 HH:m"),
           DateTimeFormatter.ofPattern("yyyy年M月dd日 HH:mm"),

           DateTimeFormatter.ofPattern("yyyy年M月d日 H:m"),
           DateTimeFormatter.ofPattern("yyyy年M月d日 H:mm"),
           DateTimeFormatter.ofPattern("yyyy年M月d日 HH:m"),
           DateTimeFormatter.ofPattern("yyyy年M月d日 HH:mm"),

           DateTimeFormatter.ofPattern("yyyy年MM月d日 H:m"),
           DateTimeFormatter.ofPattern("yyyy年MM月d日 H:mm"),
           DateTimeFormatter.ofPattern("yyyy年MM月d日 HH:m"),
           DateTimeFormatter.ofPattern("yyyy年MM月d日 HH:mm"),

            DateTimeFormatter.ofPattern("yyyy年-MM月-dd日 H:m"),
            DateTimeFormatter.ofPattern("yyyy年-MM月-dd日 H:mm"),
            DateTimeFormatter.ofPattern("yyyy年-MM月-dd日 HH:m"),
            DateTimeFormatter.ofPattern("yyyy年-MM月-dd日 HH:mm"),

            DateTimeFormatter.ofPattern("yyyy年-M月-dd日 H:m"),
            DateTimeFormatter.ofPattern("yyyy年-M月-dd日 H:mm"),
            DateTimeFormatter.ofPattern("yyyy年-M月-dd日 HH:m"),
            DateTimeFormatter.ofPattern("yyyy年-M月-dd日 HH:mm"),

            DateTimeFormatter.ofPattern("yyyy年-M月-d日 H:m"),
            DateTimeFormatter.ofPattern("yyyy年-M月-d日 H:mm"),
            DateTimeFormatter.ofPattern("yyyy年-M月-d日 HH:m"),
            DateTimeFormatter.ofPattern("yyyy年-M月-d日 HH:mm"),

            DateTimeFormatter.ofPattern("yyyy年-MM月-d日 H:m"),
            DateTimeFormatter.ofPattern("yyyy年-MM月-d日 H:mm"),
            DateTimeFormatter.ofPattern("yyyy年-MM月-d日 HH:m"),
            DateTimeFormatter.ofPattern("yyyy年-MM月-d日 HH:mm"),


            DateTimeFormatter.ofPattern("yyyyMMdd H:m"),
            DateTimeFormatter.ofPattern("yyyyMMdd H:mm"),
            DateTimeFormatter.ofPattern("yyyyMMdd HH:m"),
            DateTimeFormatter.ofPattern("yyyyMMdd HH:mm"),

           DateTimeFormatter.ofPattern("yyyy-MM-dd H:m"),
           DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm"),
           DateTimeFormatter.ofPattern("yyyy-MM-dd HH:m"),
           DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"),

           DateTimeFormatter.ofPattern("yyyy-M-dd H:m"),
           DateTimeFormatter.ofPattern("yyyy-M-dd H:mm"),
           DateTimeFormatter.ofPattern("yyyy-M-dd HH:m"),
           DateTimeFormatter.ofPattern("yyyy-M-dd HH:mm"),

            DateTimeFormatter.ofPattern("yyyyMdd H:m"),
            DateTimeFormatter.ofPattern("yyyyMdd H:mm"),
            DateTimeFormatter.ofPattern("yyyyMdd HH:m"),
            DateTimeFormatter.ofPattern("yyyyMdd HH:mm"),

           DateTimeFormatter.ofPattern("yyyy-M-d H:m"),
           DateTimeFormatter.ofPattern("yyyy-M-d H:mm"),
           DateTimeFormatter.ofPattern("yyyy-M-d HH:m"),
           DateTimeFormatter.ofPattern("yyyy-M-d HH:mm"),

            DateTimeFormatter.ofPattern("yyyyMd H:m"),
            DateTimeFormatter.ofPattern("yyyyMd H:mm"),
            DateTimeFormatter.ofPattern("yyyyMd HH:m"),
            DateTimeFormatter.ofPattern("yyyyMd HH:mm"),


           DateTimeFormatter.ofPattern("yyyy-MM-d H:m"),
           DateTimeFormatter.ofPattern("yyyy-MM-d H:mm"),
           DateTimeFormatter.ofPattern("yyyy-MM-d HH:m"),
           DateTimeFormatter.ofPattern("yyyy-MM-d HH:mm"),

            DateTimeFormatter.ofPattern("yyyyMMd H:m"),
            DateTimeFormatter.ofPattern("yyyyMMd H:mm"),
            DateTimeFormatter.ofPattern("yyyyMMd HH:m"),
            DateTimeFormatter.ofPattern("yyyyMMd HH:mm"),

           DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

   };

    public static String formatYmd(LocalDateTime date){
        return dfYMD.format(date);
    }
    
    public static String formatYm(LocalDateTime date){
        return dfYM.format(date);
    }

    public static LocalDateTime getDate(String datetime){

        if(datetime.contains("分钟前")) {
            Integer minute = Integer.parseInt(datetime.replace("分钟前",""));
            long timeStamp = System.currentTimeMillis() - minute * 60 * 1000;
            Instant instant = Instant.ofEpochMilli(timeStamp);
            return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        }else if(datetime.contains("小时前")){
            Integer house = Integer.parseInt(datetime.replace("小时前",""));
            long timeStamp = System.currentTimeMillis() - house * 60 * 60 * 1000;
            Instant instant = Instant.ofEpochMilli(timeStamp);
            return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        }
        else if(datetime.contains("昨天")){
            return LocalDateTime.of(LocalDate.now().plusDays(-1), LocalTime.MIN);
        }else if(datetime.contains("天前")){
            Integer day = Integer.parseInt(datetime.replace("天前",""));
            return LocalDateTime.of(LocalDate.now().plusDays(-day), LocalTime.MIN);
        }else if(datetime.contains("周前")){
            Integer week = Integer.parseInt(datetime.replace("周前",""));
            return LocalDateTime.of(LocalDate.now().plusWeeks(-week), LocalTime.MIN);
        }else if(datetime.contains("月前")){
            Integer month = Integer.parseInt(datetime.replace("月前",""));
            return LocalDateTime.of(LocalDate.now().plusMonths(-month), LocalTime.MIN);
        }else if(datetime.contains("年前")){
            Integer year = Integer.parseInt(datetime.replace("年前",""));
            return LocalDateTime.of(LocalDate.now().plusYears(-year), LocalTime.MIN);
        }
        if(datetime.contains("下午")) {
            String hm =   datetime.split("下午")[1];
            String hh = hm.split(":")[0];
            String mm = hm.split(":")[1];
            Integer realHh = Integer.parseInt(hh) + 12;
            datetime = datetime.split("下午")[0] + realHh + ":" + mm;
        } else if(datetime.contains("上午")) {
            datetime = datetime.replaceAll("上午","");
        }
        if(datetime.length()<=5){
            return parseFromMin(datetime);
        }
        return parse(datetime);
    }

    private static LocalDateTime parse(String datetime){
        for(DateTimeFormatter df : dfList) {
            try {
                return LocalDateTime.parse(datetime, df);
            }catch (DateTimeParseException e) {

            }
        }
       return null;
    }

    private static LocalDateTime parseFromMin(String datetime){
        LocalTime targetMin = null;
        for(DateTimeFormatter df : dfMinList) {
            try {
                targetMin = LocalTime.parse(datetime, df);
                break;
            }catch (DateTimeParseException e) {

            }
        }
        if(null != targetMin) {
           return LocalDateTime.of(LocalDate.now(),targetMin);
        }
        return null;
    }
    
    public static void main(String[] args) {
        LocalDateTime dd = DateUtil.getDate("下午8:5");
        System.out.println(df.format(dd));
        
        System.out.println(formatYm(LocalDateTime.now()));
        
    }
}
